package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("daily")
public class DailyController {
    @Autowired
    private IDailyService dailyService;

    /**
     * 查询所有日报,并且分页
     */
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        PageInfo<Daily> pageInfo = dailyService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "daily/list";
    }

    /**
     * 根据日报id,查询日报内容
     */
    @RequestMapping("selectContentById")
    @ResponseBody
    public DailyContent selectContentById(Long id) {
        DailyContent dailyContent = dailyService.selectContentById(id);
        return dailyContent;
    }

    /**
     * 新增或保存日报内容
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Daily daily, MultipartFile file) {
        JsonResult result = new JsonResult();
        try {
            dailyService.saveOrUpdate(daily,file);
        } catch (Exception e) {
            result.mark("保存或更改错误");
        }
        return result;
    }
}
